package com.sanjiahantang.youjiarole.module.category.views.impl;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.sanjiahantang.plugin.slideview.BaseSliderView;
import com.sanjiahantang.uilibrary.views.NoScrollGridView;
import com.sanjiahantang.youjiarole.R;
import com.sanjiahantang.youjiarole.base.BaseMvpFragment;
import com.sanjiahantang.youjiarole.config.ConfigValue;
import com.sanjiahantang.youjiarole.module.category.beans.CategoryBean;
import com.sanjiahantang.youjiarole.module.category.presenters.CategoryPresenter;
import com.sanjiahantang.youjiarole.module.category.views.CategoryView;
import com.sanjiahantang.youjiarole.module.goods.views.impl.GoodsListActivity;
import com.sanjiahantang.youjiarole.utils.FileUtils;
import com.squareup.picasso.Picasso;

import java.util.List;


public class CategorySubFragment extends BaseMvpFragment<CategoryView,CategoryPresenter> implements
        View.OnClickListener, BaseSliderView.OnSliderClickListener,CategoryView {

    private CategoryBean.Data mData;         //数据
    private View mRootView;
    private LinearLayout llLevel2; //商品列表
    private List<CategoryBean.Data> mCategoryInfoList;
    private ScrollView svLevel2;


    public static CategorySubFragment newInstance(CategoryBean.Data data) {
        CategorySubFragment fragment = new CategorySubFragment();
        Bundle args = new Bundle();
        args.putSerializable(ARG_PARAM1, data);
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mData = (CategoryBean.Data) getArguments().getSerializable(ARG_PARAM1);
            mCategoryInfoList = mData.getChildren();
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mRootView = inflater.inflate(R.layout.fragment_category_sub, container, false);
        return mRootView;
    }

    @Override
    public CategoryPresenter initPresenter() {
        return new CategoryPresenter();
    }

    @Override
    protected void initView() {
        LayoutInflater inflater = LayoutInflater.from(context);
        llLevel2 = (LinearLayout) mRootView.findViewById(R.id.llLevel2);
        svLevel2 = (ScrollView) mRootView.findViewById(R.id.svLevel2);
        if (mCategoryInfoList == null || mCategoryInfoList.size() == 0)
        {
            svLevel2.setVisibility(View.GONE);
        }else {
            svLevel2.setVisibility(View.VISIBLE);
            for(CategoryBean.Data cate: mCategoryInfoList)
            {
                View twoview=inflater.inflate(R.layout.item_category_level_2,null);
                TextView txtTitle = (TextView) twoview.findViewById(R.id.txtTitle);
                NoScrollGridView mRecycler = (NoScrollGridView) twoview.findViewById(R.id.rvSubCategory);


                Level3Adapter adapter=new Level3Adapter(cate.getChildren());
                mRecycler.setAdapter(adapter);
                txtTitle.setText(cate.getName());

                llLevel2.addView(twoview);
            }
        }

    }

    @Override
    protected void initData() {

    }

    @Override
    public void onResume() {
        super.onResume();

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();

    }

    @Override
    public void onDestroy() {
        super.onDestroy();

    }


    private void dispatch(String keyword){
//        Intent intent = new Intent(getContext(), GoodsFilterActivity.class);
//
//        Bundle extras = new Bundle();
//        extras.putString("keyword", keyword);
//        intent.putExtras(extras);
//
//        startActivity(intent);
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {


        }
    }

    @Override
    public void onSliderClick(BaseSliderView slider) {
        dispatch(slider.getBundle().getString("extra"));
    }

    @Override
    public void showProgress() {
        context.showProgress(context);
    }

    @Override
    public void hideProgress() {
        context.hideProgress();
    }


    public class Level3Adapter extends BaseAdapter
    {
        List<CategoryBean.Data> level3Data;
        LayoutInflater inflater;

        public Level3Adapter(List<CategoryBean.Data> level3Data)
        {
            this.level3Data = level3Data;
            inflater=LayoutInflater.from(getContext());
        }

        @Override
        public int getCount() {
            return level3Data == null ? 0 : level3Data.size();
        }

        @Override
        public Object getItem(int i) {
            return level3Data.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(final int i, View view, ViewGroup viewGroup) {
            view=inflater.inflate(R.layout.item_category_two_level_temporary,null);
            TextView tv_tname= (TextView) view.findViewById(R.id.tvTwoLevelItem);
            ImageView ivSubCate = (ImageView) view.findViewById(R.id.ivSubCate);
            Picasso.with(context).load(FileUtils.rePath(level3Data.get(i).getUrl())).placeholder(R.color.bg_mask).into(ivSubCate);
            tv_tname.setText(level3Data.get(i).getName());
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    dispatch(level3Data.get(i).getName(), level3Data.get(i).getMgcId());
                }
            });

            return view;
        }

        private void dispatch(String keyword,int id){
            Intent intent = new Intent(getContext(), GoodsListActivity.class);

            Bundle extras = new Bundle();
//        extras.putString("keyword", keyword);
            extras.putInt("id",id);
            extras.putString(KEY_TITLE,keyword);
            intent.putExtras(extras);
            getContext().startActivity(intent);
        }
    }

}
